package com.zzsy.shoppingcardemo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzsy.shoppingcardemo.adapter.ShoppingCartAdapter;
import com.zzsy.shoppingcardemo.bean.GoodsBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener{

    private TextView tvShopcartEdit;
    private RecyclerView recyclerview;
    private LinearLayout llCheckAll;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private Button btnCheckOut;
    private LinearLayout llDelete;
    private CheckBox cbAll;
    private Button btnDelete;
    private Button btnColletion;
    private LinearLayout llViewNo;
    //编辑状态
    private static final int ACTION_EDIT = 1;
    //完成状态
    private static final int ACTION_COMPLETE = 2;
    private ShoppingCartAdapter shoppingCartAdapter;
    List<GoodsBean> allData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        allData = new ArrayList<>();
        initUI();
    }

    private void initUI() {
        tvShopcartEdit =  findViewById(R.id.tv_shopcart_edit);
        recyclerview = findViewById(R.id.recyclerview);
        llCheckAll = findViewById(R.id.ll_check_all);
        checkboxAll = findViewById(R.id.checkbox_all);
        tvShopcartTotal = findViewById(R.id.tv_shopcart_total);
        btnCheckOut = findViewById(R.id.btn_check_out);
        llDelete = findViewById(R.id.ll_delete);
        cbAll = findViewById(R.id.cb_all);
        btnDelete = findViewById(R.id.btn_delete);
        btnColletion = findViewById(R.id.btn_collection);
        //没有数据的布局
        llViewNo = findViewById(R.id.ll_empty_shopcart);


        btnCheckOut.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnColletion.setOnClickListener(this);

        inistener();
    }


    @Override
    protected void onResume() {
        super.onResume();
        showData();
    }

    private void showData() {

        GoodsBean goodsBean1 = new GoodsBean();
        goodsBean1.setCover_price("159");
        goodsBean1.setFigure("http://img3.imgtn.bdimg.com/it/u=344195214,1892239641&fm=26&gp=0.jpg");
        goodsBean1.setName("时尚男士韩版T血，挥泪大甩卖");
        goodsBean1.setNumber(0);
        GoodsBean goodsBean2 = new GoodsBean();
        goodsBean2.setCover_price("1599.00");
        goodsBean2.setFigure("http://img3.imgtn.bdimg.com/it/u=2757819565,4220707122&fm=26&gp=0.jpg");
        goodsBean2.setName("时尚女性");
        goodsBean2.setNumber(0);
        GoodsBean goodsBean3 = new GoodsBean();
        goodsBean3.setCover_price("1259.00");
        goodsBean3.setFigure("http://img3.imgtn.bdimg.com/it/u=344195214,1892239641&fm=26&gp=0.jpg");
        goodsBean3.setName("时尚男士韩版T血，挥泪大甩卖");
        goodsBean3.setNumber(1);
        GoodsBean goodsBean4 = new GoodsBean();
        goodsBean4.setCover_price("9.99");
        goodsBean4.setFigure("http://img3.imgtn.bdimg.com/it/u=902518220,2743667191&fm=26&gp=0.jpg");
        goodsBean4.setName("时尚男士韩版T血，挥泪大甩卖");
        goodsBean4.setNumber(0);
        allData.add(goodsBean1);
        allData.add(goodsBean2);
        allData.add(goodsBean3);
        allData.add(goodsBean4);
        if(allData != null && allData.size()>0){
            tvShopcartEdit.setVisibility(View.VISIBLE);
            llCheckAll.setVisibility(View.VISIBLE);
            //有数据
            //把当没有数据显示的布局隐藏
            llViewNo.setVisibility(View.GONE);
            //设置适配器
            shoppingCartAdapter = new ShoppingCartAdapter(MainActivity.this,allData,tvShopcartTotal,checkboxAll,cbAll);
            recyclerview.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
            recyclerview.setAdapter(shoppingCartAdapter);
        }else{
            //没有数据 显示数据为空的页面
            emprtyShppingCart();

        }

    }

    private void emprtyShppingCart() {
        llViewNo.setVisibility(View.VISIBLE);
        tvShopcartEdit.setVisibility(View.GONE);
        llDelete.setVisibility(View.GONE);
    }


    private void inistener() {
      //设置默认状态的编辑
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int action = (int) v.getTag();
                if(action == ACTION_EDIT){
                    //切换完成状态
                    showDelete();
                }else{
                    //切换成编辑状态
                    hideDelete();
                }
            }
        });
    }

    /**
     * 编辑状态
     * */
    private void hideDelete() {
        //设置状态文本为完成装填
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setText("编辑");
        //变成非勾选
        if(shoppingCartAdapter != null){
            shoppingCartAdapter.checkAll_none(true);
            shoppingCartAdapter.checkAll();
            shoppingCartAdapter.ShowTotalPrice();
        }
        //删除的视图
        llDelete.setVisibility(View.GONE);
        //结算视图隐藏
        llCheckAll.setVisibility(View.VISIBLE);
    }

    /**
     * 完成状态
     * */
    private void showDelete() {
        //设置状态文本为完成装填
        tvShopcartEdit.setTag(ACTION_COMPLETE);
        tvShopcartEdit.setText("完成");

        //删除的视图
        llDelete.setVisibility(View.VISIBLE);
        //结算视图隐藏
        llCheckAll.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if (v == btnDelete) {
            //删除选中
            shoppingCartAdapter.deleteData();
            //校验
            shoppingCartAdapter.checkAll();
            //数据大小为0
            if(shoppingCartAdapter.getItemCount() == 0){
                emprtyShppingCart();
            }
        }
    }
}
